package forms;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import entities.AutoSite;

public class AutoSiteForm {
	public static TreeMap checkValid(HttpServletRequest request, AutoSite autoSite) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String url = (String) request.getParameter("url");
		String regEmail = (String) request.getParameter("regEmail");
		String scheduleSite = (String) request.getParameter("scheduleSite");
		String start = (String) request.getParameter("start");
		String total = (String) request.getParameter("total");
		String status = (String) request.getParameter("status");
		
		
		if (url == null || url.equals("")) {
			checkValid.put("url", "Site is not empty!");
		} else {
			try {
				URL surl = new URL(url);
			} catch (Exception e) {
				checkValid.put("url", "Site is not url valid!");
			}
			autoSite.setUrl(url);
		}
		
		if (regEmail == null || regEmail.equals("")) {
			checkValid.put("regEmail", " Selector of Email Link is not empty!");
		} else {
			autoSite.setRegEmail(regEmail);
		}
		
		
		if (scheduleSite == null || scheduleSite.isEmpty()) {
			checkValid.put("scheduleSite", "Time schedule site is not emtpy");
		} else {
			try {
				autoSite.setScheduleSite(Integer.parseInt(scheduleSite));
			} catch (Exception e) {
				checkValid.put("scheduleSite", "Time schedule site must be a number!");
			}
		}
		
		if (start == null || start.isEmpty()) {
			checkValid.put("start", "Start page is not emtpy");
		} else {
			try {
				autoSite.setStart(Integer.parseInt(start));
			} catch (Exception e) {
				checkValid.put("start", "Start page must be a number!");
			}
		}
		
		if (total == null || total.isEmpty()) {
			checkValid.put("total", "Total page is not emtpy");
		} else {
			try {
				autoSite.setTotal(Integer.parseInt(total));
			} catch (Exception e) {
				checkValid.put("total", "Total page must be a number!");
			}
		}
		
		if (status == null || status.isEmpty()) {
			checkValid.put("status", "Status is not emtpy");
		} else {
			try {
				autoSite.setStatus(Integer.parseInt(status));
			} catch (Exception e) {
				checkValid.put("status", "Status must be a number!");
			}
		}
		
		form.put("checkValid", checkValid);
		form.put("autoSite", autoSite);
		return form;
	}
	
}
